//    jen-breakout is the game of breakout implemented in C, using SDL.
//    Copyright 2009 Nathan Michaels
//
//    This file is part of jen-breakout.
//
//    jen-breakout is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 2 of the License, or
//    (at your option) any later version.
//
//    jen-breakout is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with jen-breakout.  If not, see <http://www.gnu.org/licenses/>.

#include <string.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include "types.h"
#include "breakout.h"
#include "highscores.h"

#define SCORES_FILE "scores"

high_score_t highScores[NUM_HIGH_SCORES];

// Note that name should actually be a pointer to the name field of a high score
// and thus NAME_LEN + 1 long. We shouldn't rely on this though. Also note that
// C does not allow arrays as parameters to functions, so sizeof() will not work
// on name.
void GetName (char name[NAME_LEN])
{
    SDL_Event event;
    boolean_t shifted;
    int  idx;
    char ch;

    // Let SDL do the converting for us.
    SDL_EnableUNICODE (1);

    // This should probably have already happened, but let's just stay on the
    // safe side.
    memset (name, 0, NAME_LEN);

    // It's like a mini main loop!
    idx = 0;
    shifted = FALSE;

    while (TRUE)
    {
        // Check for input
        while (SDL_PollEvent (&event))
        {
            ch = 0;
            switch (event.type)
            {
            case SDL_QUIT:
                // Whoops, someone closed the window!
                // Let that bubble on up.
                SDL_PushEvent (&event);
                return;
            case SDL_KEYDOWN:
                // We really only care about the down event.
                switch (event.key.keysym.sym)
                {
                case SDLK_RETURN:
                    // All done!
                    RenderMessage (" ");
                    RenderName (" ");
                    return;
                case SDLK_BACKSPACE:
                    idx--;
                    if (idx == 0)
                    {
                        RenderName (" ");
                    }
                    else
                    {
                        name[idx] = 0;
                        RenderName (name);
                    }
                    break;
                case SDLK_CLEAR:
                case SDLK_PAUSE:
                case SDLK_ESCAPE:
                case SDLK_TAB:
                case SDLK_DELETE:
                    // These keysyms don't get handled at all.
                    break;
                default:
                    // If it's an ascii character, use that. Otherwise, ignore
                    // it.
                    if (event.key.keysym.sym <= 0x7F)
                        ch = (char)event.key.keysym.unicode;
                }

                // If there was any, handle it.
                if (ch != 0)
                    name[idx++] = ch;

                if (idx == NAME_LEN - 1)
                    idx = NAME_LEN - 2;
            }

            RenderMessage ("You got a high score! Enter your name!");
            
            // Render Callback
            if (idx != 0)
                RenderName (name);
        }
    }
}


void WriteHighScores (void)
{
    int idx;
    FILE *filePtr;

    filePtr = fopen (SCORES_FILE, "wb");

    for (idx = 0; idx < NUM_HIGH_SCORES; idx++)
        fprintf (filePtr, "%s\x04%u\n", highScores[idx].name,
                 highScores[idx].score);

    fclose (filePtr);
}

status_t InitHighScores (void)
{
    int idx, result, chIdx;
    char ch;
    FILE *filePtr;

    memset (highScores, 0, sizeof (highScores));
    for (idx = 0; idx < NUM_HIGH_SCORES; idx++)
        highScores[idx].name[0] = ' ';

    // Read high scores.
    filePtr = fopen (SCORES_FILE, "rb");
    if (filePtr == NULL)
        return SUCCESS;

    for (idx = 0; idx < NUM_HIGH_SCORES; idx++)
    {
        for (chIdx = 0; chIdx < NAME_LEN; chIdx++)
        {
            ch = fgetc (filePtr);
            if (ch != EOF &&
                ch != '\x04')
                highScores[idx].name[chIdx] = ch;
            else
                break;
        }

        result = fscanf (filePtr, "%u\n", &highScores[idx].score);

        if (result == EOF)
            break;
        if (result != 1)
            return FAILURE;
    }

    fclose (filePtr);
    return SUCCESS;
}
